package com.da.javatest.leetCode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author chenlida
 * @date 2019/2/15 09:39
 * @description
 */
public class Code989 {
    public static void main(String[] args) {
        System.out.println(new Code989().addToArrayForm(new int[] {1}, 33));
    }

    public List<Integer> addToArrayForm(int[] A, int K) {
        List<Integer> result = new ArrayList<>();
        if (A.length < (K + "").length()) {
            int sum = 0;
            for (int i : A) {
                sum = sum * 10 + i;
            }
            sum += K;
            while (sum != 0) {
                result.add(sum % 10);
                sum /= 10;
            }
            Collections.reverse(result);
            return result;
        }

        boolean flag = false;
        for (int i = A.length - 1; i >= 0; i--) {
            int j = K % 10;
            int sum = A[i] + j;
            if (flag) {
                sum++;
                flag = false;
            }
            if (sum > 9) {
                sum -= 10;
                flag = true;
            }
            result.add(sum);
            K /= 10;
        }
        if (flag) {
            result.add(1);
        }
        Collections.reverse(result);
        return result;
    }
}
